/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaproject;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

/**
 *
 * @author hanxiao
 */
public class Window extends JFrame implements ActionListener{
    private JTextArea checkScore;   
    private JTextArea  TestField;
    private JButton Score;
    private JButton start;
    private JButton end;
    private JButton ReadUp;
    private JButton ReadNext;
    private JButton ButtonA, ButtonB, ButtonC, ButtonD;
    private JLabel head;
    int index =0;
    int count=0;  //add points
    preWindow p;
    UserInfo use;
    String str = null;
    ArrayList <QuestionInfo> questionList = new  ArrayList <QuestionInfo>();
    ArrayList<UserInfo> User = new ArrayList<UserInfo>();  
    public final  int ADDSCORE =10;
    public Window(String s){
        str =s;
        use = new UserInfo();
        p = new preWindow();
        setTitle("Quiz System");
        setBounds(400,200,800,470);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel North=new JPanel();
        North.setLayout(new BorderLayout());
        JLabel userInfo =new JLabel("Name"+": "+str,SwingConstants.LEFT);
        userInfo.setForeground(Color.BLUE);
        userInfo.setFont(new Font("Arial",Font.BOLD,20));
        head =new JLabel("Welcome Quiz Interface",SwingConstants.CENTER);
        head.setForeground(Color.blue);
        head.setFont(new Font("Arial",Font.BOLD,36));
        North.add(userInfo,BorderLayout.WEST);
        North.add(head,BorderLayout.CENTER);
        
        
        JPanel West=new JPanel();
        West.setLayout(new BorderLayout());
        checkScore=new JTextArea(14,16);
        checkScore.setEditable(false);
        Score=new JButton("Check Score");
        Score.addActionListener(this);
        West.add(checkScore);
        West.add(Score, BorderLayout.SOUTH);
        West.setBorder(new TitledBorder("Work Field"));
    
        JPanel Center=new JPanel();
        Center.setLayout(new FlowLayout());
        TestField=new JTextArea(20,30);
        JPanel j = new JPanel();    
        j.setLayout(new FlowLayout());
        start = new JButton("Start");
        start.addActionListener(this);
        end = new JButton("End");
        end.addActionListener(this);
        j.add(start);
        j.add(new JLabel("         "));
        j.add(end);
        Center.add(TestField);
        Center.add(j);
        Center.setBorder(new TitledBorder("Question Field"));
        TestField.setEditable(false);
 
        JPanel East =new JPanel();
        East.setLayout(new GridLayout(5,1));
        ButtonA = new JButton("A");
        ButtonA.addActionListener(this);
        ButtonB = new JButton("B");
        ButtonB.addActionListener(this);
        ButtonC = new JButton("C");
        ButtonC.addActionListener(this);
        ButtonD = new JButton("D");
        ButtonD.addActionListener(this);
            East.add(ButtonA);
            East.add(ButtonB);
            East.add(ButtonC);
            East.add(ButtonD);
        JPanel ins = new JPanel();
        ins.setLayout(new FlowLayout());
//        ReadUp=new JButton("Last Question");
//        ins.add(ReadUp);
//        ReadUp.addActionListener(this);
        ReadNext=new JButton("Next Question");
        ReadNext.addActionListener(this);
        ins.add(ReadNext);
        East.setEnabled(false);
        East.setBorder(new TitledBorder("Select Field"));
        East.add(ins);
        
        this.setLayout(new BorderLayout());
        this.add(North, BorderLayout.NORTH);
        this.add(Center, BorderLayout.CENTER);
        this.add(West,BorderLayout.WEST);
        this.add(East,BorderLayout.EAST);
       
    }
    
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println(ae.getActionCommand());
        String temQuestion = "";
        String temChoiceA = "";
        String temChoiceB = "";
        String temChoiceC = "";
        String temChoiceD = "";
        int temId = 0;
        if(ae.getActionCommand()=="Start"){  
//            ReadUp.setEnabled(true);
            ReadNext.setEnabled(true);
            Connection con = ImportDb.getConnection();
            Statement st = null;
            ResultSet rs = null;
            
            String s1 = "select id, question, choiceA, choiceB, choiceC, choiceD from information";
            try{
                st = con.createStatement();
                rs = st.executeQuery(s1);
               
                while(rs.next()){
                    int id = rs.getInt(1);
                    temId = id;
                    String question = rs.getString(2);
                    temQuestion = question;
                    String a = rs.getString(3);
                    temChoiceA = a;
                    String b = rs.getString(4);
                    temChoiceB = b;
                    String c = rs.getString(5);
                    temChoiceC = c;
                    String d = rs.getString(6);
                    temChoiceD = d;                    
                    questionList.add(new QuestionInfo(temId,temQuestion,temChoiceA, temChoiceB, temChoiceC, temChoiceD));
                }
                index = 0;
                TestField.setText(questionList.get(0).toString());
            }catch (SQLException ex) {
              System.out.println("start method error!  "+ex);
              
            }finally{
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println(ex+"close connection error!!");
                }
            }
        }
        if(ae.getActionCommand()=="Next Question"){
            //ReadUp.setEnabled(false);
            if(index<questionList.size()-1){
                int tempId = ++index;
                System.out.println("tempId = "+index);
                QuestionInfo tem = questionList.get(tempId);
                TestField.setText(tem.toString());
            }else{
                index = questionList.size()-1;
                System.out.println("index:   "+index);
                JOptionPane.showMessageDialog(Window.this, "It is the last question!");
                
            }  
        }
//        if(ae.getActionCommand() == "Last Question"){
//            if(index >0){
//                int tempId = --index;
//                System.out.println("tempId = "+tempId);
//                QuestionInfo tem = questionList.get(tempId);
//                TestField.setText(tem.toString());
//              
//            }else{
//                index = 0;
//                JOptionPane.showMessageDialog(Window.this, "It is the first question!");
//            }
//        }
        if(ae.getSource()==ButtonA || ae.getSource()==ButtonB || ae.getSource()==ButtonC || ae.getSource()==ButtonD || ae.getActionCommand() == "End"){
             int array=0;
             Connection con = ImportDb.getConnection();
             Statement st = null;
             ResultSet rs = null;
             String sql = "select answer from information";
             String temResult = "";
             try {
                st = con.createStatement();
                rs = st.executeQuery(sql);
                while(rs.next()){
                    String result = rs.getString("answer");
                    temResult = result;
                    questionList.add(new QuestionInfo(temResult));
                }
                if(ae.getSource()==ButtonA){
                    String a = "A";
                    if(a.equals(temResult)){
                        count = count+ADDSCORE;
                    }
                    array++;
                    System.out.println(count);
                }
                if(ae.getSource()==ButtonB){
                    String a = "B";
                    if(a.equals(temResult)){
                        count = count+ADDSCORE;
                    }
                    array++;
                    System.out.println(count);
                }
                if(ae.getSource()==ButtonC){
                    String a = "C";
                    if(a.equals(temResult)){
                        count = count+ADDSCORE;
                    }
                    array++;
                    System.out.println(count);
                }
                if(ae.getSource()==ButtonD){
                    String a = "D";
                    if(a.equals(temResult)){
                        count = count+ADDSCORE;
                    }
                    array++;
                    System.out.println(count);
                }
                
                if(ae.getActionCommand() == "End"){
                    JOptionPane.showMessageDialog(Window.this, " You have finished the quiz!\n  Your points: " + count);
    //                ReadUp.setEnabled(false);
                    ReadNext.setEnabled(false);
                }
            } catch (SQLException ex) {
                 System.out.println("abcd,end exception"+ex);
            }
        }        
        
        if(ae.getSource()==checkScore){
            checkScore.setText("name: "+str+"score: "+count);
        }
    
        if(ae.getSource()==Score){
            
            System.out.println(count);
            Connection con = ImportDb.getConnection();
            Statement st = null;
            ResultSet rs = null;
            String sql = "update user set user_point='"+count+"' where user_name='"+str+"'";
            try {
                st = con.createStatement();
                st.executeUpdate(sql);
                System.out.println("name: "+str+" count: "+count);
                
            } catch (SQLException ex) {
                System.out.println("score error" + ex);
            }
        }

    }
}